package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper mapper;
    public void saveOrUpdate(Role u,int []ids) {
        if(u.getId()==null){
            mapper.insert(u);
        }else{
            mapper.deletePermission(u.getId());
            mapper.updateByPrimaryKey(u);
        }
        if (ids != null && ids.length >= 0) {
            for (int id : ids) {
                mapper.addPermission(u.getId(), id);
            }
        }
    }


    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
        mapper.deleteRelation(id);
        mapper.deletePermission(id);
    }


    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> result = mapper.queryForList(qo);
        return new PageInfo(result);
    }


    public Role selectOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Role> listAll() {
        return mapper.selectAll();
    }


}
